package com.xb.Demo01;


import com.mysql.cj.jdbc.Driver;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 测试连接的
 */
public class Demo01TestConnection {
    //方式一：
    @Test
    public void testConnection() throws SQLException {
        Driver driver = new Driver();
        String url = "jdbc:mysql://localhost:3306/test";
        Properties properties =new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        // 创建连接对象
        Connection connect = driver.connect(url, properties);

        System.out.println(connect);
        connect.close();
    }
    //方式二：
    @Test
    public void testConnection02() throws SQLException {
        //new com.mysql.jdbc.Driver() 第三方的 通过反射获取
        //这里设置为反射的话 虽然代码看上去还是写死的，但是其实可以通过配置文件来进行读取的这样就
        //能解决开闭和问题
        Driver driver= null;
        try {
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            driver= (Driver)clazz.getDeclaredConstructor().newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/test";
        Properties properties =new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","123456");
        // 创建连接对象
        Connection connect = driver.connect(url, properties);
        System.out.println(connect);
        connect.close();
    }

    //方式三：
    @Test
    public void testConnection03() throws SQLException {
        //使用DriverManager
        Driver driver= null;
        try {
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            driver= (Driver)clazz.getDeclaredConstructor().newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        //注册驱动
        DriverManager.registerDriver(driver);
        // 创建连接对象
        Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        System.out.println(connect);
        connect.close();
    }

    //方式四：
    @Test
    public void testConnection04() throws SQLException, ClassNotFoundException {
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "123456";
        //反射会把类加载到内存中
        /**
         * 在MYSQL的静态代码块里面会默认加载这个驱动到内存中的
         *  static {
         *         try {
         *             DriverManager.registerDriver(new Driver());
         *         } catch (SQLException var1) {
         *             throw new RuntimeException("Can't register driver!");
         *         }
         *     }
         */
        Class.forName("com.mysql.jdbc.Driver");

        // 创建连接对象
        Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        System.out.println(connect);
        connect.close();
    }
    //方式五 读配置文件：
    @Test
    public void testConnection05() throws SQLException, ClassNotFoundException, IOException {
        InputStream resourceAsStream = Demo01TestConnection.class.getClassLoader().getResourceAsStream("mysqlConnectInfo.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Class.forName(driver);
        // 创建连接对象
        Connection connect = DriverManager.getConnection(url, user, password);
        System.out.println(connect);
        connect.close();
    }
}
